<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/20
 * Time: 17:17
 */

namespace zhtfrontend\controllers;

use common\components\helpers\Fun;
use yii\web\BadRequestHttpException;
use yii\web\Response;
use yii\widgets\ActiveForm;
use zhtfrontend\components\bases\BaseController;
use zhtfrontend\models\datas\NavMenu;
use yii\data\Pagination;

class NavmenuController extends BaseController
{
    //班主任列表
    public function actionIndex()
    {
        $search_form = new NavMenu();
        if (\Yii::$app->request->isPost) {
            $search_form->load(\Yii::$app->request->post());
        }
        $search_data = array_filter($search_form->attributes);
        $model = NavMenu::find()->orderBy('id DESC');
        if (!empty($search_data['name'])) {
            $model->andWhere(['like', 'name', $search_data['name']]);
        }
        if (!empty($search_data['status'])) {
            $model->andWhere(['status'=>$search_data['status']]);
        }
        if(isset(\Yii::$app->user->identity->role) && \Yii::$app->user->identity->role == 'type'){
            $model->andWhere(['type_id'=>\Yii::$app->user->identity->type_id]);
        }
        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => 14, 'params' => array_merge($_GET, $search_data)]);
        $data = $model->offset($pages->offset)->limit($pages->limit)->all();
        $data = Fun::asArray($data, ['operate']);
        return $this->render('index', ['model' => $data, 'form' => $search_form, 'pages' => $pages]);
    }

    public function actionCreate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $model = new NavMenu();
        $model->status = 1;

        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        $typeArr = [1=>'益智',2=>'音乐',3=>'形体'];
        if(\Yii::$app->user->identity->role && \Yii::$app->user->identity->role == 'type'){
            $typeArr = [\Yii::$app->user->identity->type_id=>$typeArr[\Yii::$app->user->identity->type_id]];
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->admin_id = \Yii::$app->user->id;
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['navmenu/index']));
            }
        }
        return $this->render('create', ['model' => $model,'typearr'=>$typeArr]);
    }

    public function actionUpdate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $id = \Yii::$app->request->get('id');
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model = NavMenu::findOne($id);
        if ($model === null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        if(\Yii::$app->user->identity->role && \Yii::$app->user->identity->role == 'type'){
            if($model->type_id != \Yii::$app->user->identity->type_id){
                throw new BadRequestHttpException(\Yii::t('zhtfrontend', '没权限'), 404);
            }
        }
        $typeArr = [1=>'益智',2=>'音乐',3=>'形体'];
        if(\Yii::$app->user->identity->role && \Yii::$app->user->identity->role == 'type'){
            $typeArr = [\Yii::$app->user->identity->type_id=>$typeArr[\Yii::$app->user->identity->type_id]];
        }
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            if ($model->save(false)) {
                return $this->redirectMessage('修改成功', Fun::url(['navmenu/index']));
            }
        }
        return $this->render('create', ['model' => $model,'typearr'=>$typeArr]);
    }

}